package com.mq.rabbitmq.deadletter;

import com.google.common.collect.ImmutableMap;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;

/**
 * Created by Administrator on 2017/8/3.
 * 死信的场景：
 * 消息被拒绝（basic.reject/ basic.nack）并且requeue=false
   消息TTL参考：RabbitMQ之TTL（Time-To-Live 过期时间））
   队列达到最大长度
 *
 */
//@Configuration
@Import(value = {DealLetetterReceive.class,DealSend.class})
public class DealLetterConfig {

    public static final String DEFAULT_DEAD_EXCHANGE = "dead.letter.exchange";
    public static final String DEAD_QUEUE = "dead.queue";
    public static final String DEAD_ROUTE_KEY = DEAD_QUEUE;


    //这是个正常的对列，当对列中的消息不满足的时候，会触发这个对列绑定的exchange,和queue
    public static final String  QUEUE_DEAD_TEST ="dead.test.queue";
    public static final String  QUEUE_EXCHANGE ="dead.test.exchange";


    @Bean
    public DirectExchange deadDirectExchange() {
        return new DirectExchange(DEFAULT_DEAD_EXCHANGE);
    }


    @Bean
    public Queue deadLetterQueue() {
        return new Queue(DEAD_QUEUE);
    }

    @Bean
    public Binding deadLetterBinding() {
        return BindingBuilder.bind(deadLetterQueue()).to(deadDirectExchange()).with(DEAD_ROUTE_KEY);
    }


    //上面三个是定义死信对列的属性
    //这个是定义正常对列功能
    //往死信定义过的队列放置
    @Bean
    public DirectExchange tradeQueueExchange() {
        return new DirectExchange(QUEUE_EXCHANGE);
    }


    @Bean
    public Queue tradeQueue() {
//        final ImmutableMap<String, Object> args =
//                ImmutableMap.of("x-dead-letter-exchange", DEFAULT_DEAD_EXCHANGE,
//                        "x-dead-letter-routing-key", DEAD_ROUTE_KEY);
        final ImmutableMap<String, Object> args =
                ImmutableMap.of("x-dead-letter-exchange", DEFAULT_DEAD_EXCHANGE,
                        "x-dead-letter-routing-key", DEAD_ROUTE_KEY);
        //这里的参数，exchange,要是死信的exchagne,routekey,要能找到死信中的queue;

        //这里要是死信队列的绑定routkey
        return new Queue(QUEUE_DEAD_TEST, true, false, false, args);
    }

    @Bean
    public Binding tradeBinding() {
        return BindingBuilder.bind(tradeQueue()).to(tradeQueueExchange()).with(tradeQueue().getName());
    }




//    @Bean
//    public Queue dealQueue() {
//        Map<String,Object> args = new HashMap<String,Object>();
//        args.put("x-message-ttl",6000);
//        args.put("x-dead-letter-exchange",DEAD_EXCHAGNE);//当对列上有死信的时候，会把数据
//
//        /**
//         *
//         * 也是一个正常的Exchange，和一般的Exchange没有区别，它能在任何的队列上被指定，实际上就是设置某个队列的属性，
//         * 当这个队列中有死信时，RabbitMQ就会自动的将这个消息重新发布到设置的Exchange上去，进而被路由到另一个队列，
//         * 可以监听这个队列中消息做相应的处理，这个特性可以弥补RabbitMQ 3.0以前支持的immediate参数
//         *
//         *
//         */
//        args.put("x-dead-letter-routing-key","queue.dlx.test");//路由键盘，死信的时候的路由键盘
//        Queue ue =  new Queue(AmqpConfig.QUEUE_DEAD, true,false,false,args); //队列持久
//        return ue;
//    }


}
